<h3>Manage Shopping Catalog</h3>
<div id="div-objects-combo"></div>
<div id="div-fields"></div>
<div id="attrib-form-div">
	<form id="attrib-form" action="#">
	
	</form>
</div>

<script type="text/javascript">

function loaditems(objid){
	var dataString='&objid='+objid;
	$.ajax({
		type: "POST",
			url: "index.php/xmlrpc_client/get_all_items",
			data: dataString,
			dataType: 'json',
			success : function(data){
				console.log(data[0]);
				var text='<div>';
				text=text+'<ul style="background-color:#AAA" class="fielddiv ui-widget ui-helper-clearfix" >';
				text=text+'<li  >&nbsp;<b>E</b>&nbsp;</li>'
			    +'<li  >&nbsp;<b>D</b>&nbsp;</li>';
				for(var hkey in data[0]){
					text=text+'<li style="width:10%"><b>'+hkey+'</b></li>';
				}
				text=text+'</ul>';
				$.each(data,function(key,val){
					console.log(key+'--'+val);

					text=text+'<ul style="background-color:#AAA" class="fielddiv ui-widget ui-helper-clearfix" >';
					text=text+'<li class="ui-state-default ui-corner-all editfield" attrid='+val.attrid+' ><span class="ui-icon ui-icon-pencil"></span></li>'
				    +'<li class="ui-state-default ui-corner-all delfield" attrid='+val.attrid+' ><span class="ui-icon ui-icon-trash"></span></li>';
					
					for(var key1 in val){
						console.log(">>>"+key1);
						text=text+'<li style="width:10%">'+val[key1]+'</li>';
					}
				    text=text+'</ul>';
				});

				text=text+'</div>';
				$('#div-fields').html(text);
			}

	});
}

function loadFields(objid){
	var dataString='&objid='+objid;
	$.ajax({
		type: "POST",
			url: "index.php/xmlrpc_client/get_all_attributes",
			data: dataString,
			dataType: 'json',
			success : function(data){
				var text='';
				if(data.length > 0){ 
					
					$.each(data,function(key,val){
						text=text+'<input type="text" id="'+val.id+'" name="'+val.id+'" value="'+val.attribute_name+'"/>';
					});

					text=text+'<button id="add-attrib-btn">Add Item</button>';
				}
				

				$('#attrib-form').html(text);

				$('#add-attrib-btn').button().click(function(event){
					event.preventDefault();
					var datastring=$('#attrib-form').serialize();
					datastring=datastring+'&objid='+objid;
					$.ajax({
						type: "POST",
						url: "index.php/xmlrpc_client/create_item",
						data: datastring,
						success:function(data){
							$.jGrowl("Item added.!");
							loaditems(objid);
							return false;
						}
					});
				});
			}
	});
}

$.getJSON('index.php/xmlrpc_client/get_all_objects', function(data) {

	   var text='<select id="objects-combobox"><option value="NONE">--SELECT--</option>';
   
	   $.each(data, function(key, val) {
	      text=text+'<option value="'+val.id+'">'+val.name+'</option>';
	   });
	   text=text+'</select>';

	   $('#div-objects-combo').html(text);

	    $('#objects-combobox').change(function(){
		    loaditems($(this).val());
		   loadFields($(this).val());
	    });

});

</script>
